﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace Amgine
{
    public partial class MainForm : Form
    {
        #region public

        private List<Dictionary> dictionaryList;
        private List<Note> noteList;
        private List<LinkmanGroup> linkmanGroupList;
        private Hashtable linkmanList;
        private Hashtable linkmanDetailsList;
        private List<ToolGroup> toolGroupList;
        private Hashtable toolList;
        private List<AccountGroup> accountGroupList;
        private Hashtable accountList;

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            ValidatePermissionForm dialog = new ValidatePermissionForm();
            DialogResult dr = dialog.ShowDialog(this);
            if (dr != DialogResult.OK)
            {
                Environment.Exit(0);
            }
            else
            {
                dialog.Dispose();
            }
        }

        private void mainTabControl_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (mainTabControl.SelectedIndex)
            {
                case 0: break;
                case 1:
                    loadLinkmanData();
                    break;
                case 2:
                    loadAccountData();
                    break;
                case 3:
                    loadToolsData();
                    break;
                case 4:
                    loadDictionaryData();
                    break;
                case 5:
                    loadNoteData();
                    break;
                default: break;
            }
        }

        private void deleteAllChildrenByTableName(string tableName)
        {
            DeleteAllChildrenForm dialog = new DeleteAllChildrenForm(tableName);
            DialogResult dr = dialog.ShowDialog(this);
            if (dr == DialogResult.OK)
            {
                if (DBHelper.TABLE_ACCOUNT.Equals(tableName))
                {
                    loadAccountData();
                }
                else if (DBHelper.TABLE_LINKMAN.Equals(tableName))
                {
                    loadLinkmanData();
                }
                else if (DBHelper.TABLE_TOOL.Equals(tableName))
                {
                    loadToolsData();
                }
                MessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("删除失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            dialog.Dispose();
        }

        #endregion

        #region dictionary

        private void loadDictionaryData()
        {
            TreeNode node = null;
            dictionaryList = DBHelper.listAllDictionary();
            treeView_dictionary.Nodes.Clear();
            foreach (Dictionary dictionary in dictionaryList)
            {
                node = new TreeNode(dictionary.Info);
                node.Nodes.Add(new TreeNode("名称：" + dictionary.Info));
                node.Nodes.Add(new TreeNode("值：" + dictionary.Code));
                node.Nodes.Add(new TreeNode("类型：" + dictionary.getTypeString()));
                node.Nodes.Add(new TreeNode("是否加密：" + (dictionary.IsEncrypted ? "是" : "否")));
                node.Tag = dictionary;
                node.ContextMenuStrip = contextMenuStrip_dictionary_node;
                treeView_dictionary.Nodes.Add(node);
            }
        }

        private void contextMenuStrip_dictionary_node_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (e.ClickedItem.Equals(toolStripMenuItem_dictionary_delete))
            {
                DialogResult dr = MessageBox.Show("确定删除记录【"
                    + ((Dictionary)treeView_dictionary.SelectedNode.Tag).Code
                    + "】吗？该操作不可撤销。", "确认删除",
                    MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (dr == DialogResult.OK)
                {
                    if (((Dictionary)treeView_dictionary.SelectedNode.Tag).delete())
                    {
                        loadDictionaryData();
                        MessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("删除失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }

                }
            }
            else if (e.ClickedItem.Equals(toolStripMenuItem_dictionary_edit))
            {
                showEditDictionaryDialog((Dictionary)treeView_dictionary.SelectedNode.Tag);
            }
        }

        private void treeView_dictionary_MouseClick(object sender, MouseEventArgs e)
        {
            treeView_dictionary.SelectedNode = treeView_dictionary.GetNodeAt(new Point(e.X, e.Y));
        }

        private void showEditDictionaryDialog(Dictionary tag)
        {
            DictionaryEditForm dialog = new DictionaryEditForm(tag);
            DialogResult dr = dialog.ShowDialog(this);
            switch (dr)
            {
                case DialogResult.OK:
                    loadDictionaryData();
                    MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                case DialogResult.Retry:
                    MessageBox.Show("保存失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                default: break;
            }
            dialog.Dispose();
        }

        private void button_new_dictionary_Click(object sender, EventArgs e)
        {
            showEditDictionaryDialog(null);
        }

        private void button_refresh_dictionary_Click(object sender, EventArgs e)
        {
            loadDictionaryData();
        }

        private void button_delete_all_dictionary_Click(object sender, EventArgs e)
        {
            if (dictionaryList.Count <= 0)
            {
                MessageBox.Show("表中已没有记录", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            DialogResult dr = MessageBox.Show("确定删除所有记录吗？该操作不可撤销。",
                "确认删除", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                if (DBHelper.deleteAllDictionary())
                {
                    loadDictionaryData();
                    MessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("删除失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        #endregion

        #region help&settings

        private void pictureBox_main_Click(object sender, EventArgs e)
        {
            MessageBox.Show("没事戳我干什么", "哎呦", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void button_reset_psw_Click(object sender, EventArgs e)
        {
            string oldPsw = Properties.Settings.Default.user_password;
            ResetUserPasswordForm dialog = new ResetUserPasswordForm();
            DialogResult dr = dialog.ShowDialog(this);
            if (dr == DialogResult.OK)
            {
                resetDBByUserPassword(oldPsw, Properties.Settings.Default.user_password);
            }
            dialog.Dispose();
        }

        private void resetDBByUserPassword(string oldPsw, string newPsw)
        {
            UpdateDBProcessForm dialog = new UpdateDBProcessForm();
            dialog.setResetPassword(oldPsw, newPsw);
            dialog.ShowDialog(this);
            dialog.Dispose();
        }

        private void button_settings_Click(object sender, EventArgs e)
        {
            SettingsForm dialog = new SettingsForm();
            DialogResult dr = dialog.ShowDialog(this);
            if (dr == DialogResult.OK)
            {
                doSomethingByCurrentSettings();
            }
            dialog.Dispose();
        }

        private void doSomethingByCurrentSettings()
        {
            // do nothing
        }

        private void button_import_Click(object sender, EventArgs e)
        {
            DialogResult dr = openImportFileDialog.ShowDialog(this);
            if (dr == DialogResult.OK)
            {
                UpdateDBProcessForm dialog = new UpdateDBProcessForm();
                dialog.setImport(openImportFileDialog.FileName);
                dialog.ShowDialog(this);
                dialog.Dispose();
            }
        }

        private void button_export_Click(object sender, EventArgs e)
        {
            DialogResult dr = saveExportFileDialog.ShowDialog(this);
            if (dr == DialogResult.OK)
            {
                UpdateDBProcessForm dialog = new UpdateDBProcessForm();
                dialog.setExport(saveExportFileDialog.FileName);
                dialog.ShowDialog(this);
                dialog.Dispose();
            }
        }

        private void button_help_Click(object sender, EventArgs e)
        {
            string path = "readme.txt";
            if (File.Exists(path))
            {
                System.Diagnostics.Process.Start(path);
            }
            else
            {
                MessageBox.Show("你TM把readme.txt文件放到哪里去了", "卧槽", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button_about_Click(object sender, EventArgs e)
        {
            MessageBox.Show("作者：Yukishige Unemenokami Mochidzuki" + Environment.NewLine + Environment.NewLine
                + "其它信息一律不提供，估计你也没啥兴趣吧", "人物情报", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void button_cheat_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.t66y.com");
        }

        private void button_quit_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        #endregion

        #region note

        private void loadNoteData()
        {
            TreeNode node = null;
            noteList = DBHelper.listAllNote();
            treeView_note.Nodes.Clear();
            foreach (Note note in noteList)
            {
                node = new TreeNode(note.Title);
                node.Nodes.Add(new TreeNode("标题：" + note.Title));
                node.Nodes.Add(new TreeNode("内容：" + (note.Info.Length > 20 ?
                    (note.Info.Substring(0, 20) + "...") : note.Info)));
                node.Nodes.Add(new TreeNode("创建时间：" + note.CreateTime));
                node.Nodes.Add(new TreeNode("修改时间：" + note.UpdateTime));
                node.Tag = note;
                node.ContextMenuStrip = contextMenuStrip_note_node;
                treeView_note.Nodes.Add(node);
            }
        }

        private void treeView_note_MouseClick(object sender, MouseEventArgs e)
        {
            treeView_note.SelectedNode = treeView_note.GetNodeAt(new Point(e.X, e.Y));
        }

        private void button_refresh_note_Click(object sender, EventArgs e)
        {
            loadNoteData();
        }

        private void button_new_note_Click(object sender, EventArgs e)
        {
            showEditNoteDialog(null, true);
        }

        private void button_delete_all_note_Click(object sender, EventArgs e)
        {
            if (noteList.Count <= 0)
            {
                MessageBox.Show("表中已没有记录", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            DialogResult dr = MessageBox.Show("确定删除所有记录吗？该操作不可撤销。",
                "确认删除", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                if (DBHelper.deleteAllNote())
                {
                    loadNoteData();
                    MessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("删除失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        private void contextMenuStrip_note_node_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (e.ClickedItem.Equals(toolStripMenuItem_note_delete))
            {
                DialogResult dr = MessageBox.Show("确定删除记录【"
                    + ((Note)treeView_note.SelectedNode.Tag).Title
                    + "】吗？该操作不可撤销。", "确认删除",
                    MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (dr == DialogResult.OK)
                {
                    if (((Note)treeView_note.SelectedNode.Tag).delete())
                    {
                        loadNoteData();
                        MessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("删除失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }

                }
            }
            else if (e.ClickedItem.Equals(toolStripMenuItem_note_edit))
            {
                showEditNoteDialog((Note)treeView_note.SelectedNode.Tag, true);
            }
            else if (e.ClickedItem.Equals(toolStripMenuItem_note_watch))
            {
                showEditNoteDialog((Note)treeView_note.SelectedNode.Tag, false);
            }
        }

        private void showEditNoteDialog(Note tag, bool canEdit)
        {
            NoteEditForm dialog = new NoteEditForm(tag, canEdit);
            DialogResult dr = dialog.ShowDialog(this);
            if (canEdit)
            {
                switch (dr)
                {
                    case DialogResult.OK:
                        loadNoteData();
                        MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        break;
                    case DialogResult.Retry:
                        MessageBox.Show("保存失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        break;
                    default: break;
                }
            }
            dialog.Dispose();
        }


        #endregion

        #region linkman

        private void loadLinkmanData()
        {
            List<Linkman> list = null;
            List<LinkmanDetails> details = null;
            TreeNode groupNode = null;
            TreeNode node = null;
            TreeNode detailsNode = null;
            TreeNode detailsType = null;
            int type = -1;

            linkmanGroupList = DBHelper.listAllLinkmanGroup();
            linkmanList = DBHelper.listAllLinkman();
            linkmanDetailsList = DBHelper.listAllLinkmanDetails();

            treeView_linkman.Nodes.Clear();
            foreach (LinkmanGroup group in linkmanGroupList)
            {
                groupNode = new TreeNode(group.Name + "：" + group.Remark);
                list = (List<Linkman>)linkmanList[group.ID];
                if (null != list)
                {
                    foreach (Linkman linkman in list)
                    {
                        node = new TreeNode(linkman.Name + "：" + linkman.Remark);
                        details = (List<LinkmanDetails>)linkmanDetailsList[linkman.ID];
                        if (null != details)
                        {
                            type = -1;
                            foreach (LinkmanDetails detail in details)
                            {
                                if (detail.Type != type)
                                {
                                    type = detail.Type;
                                    detailsType = new TreeNode(detail.getTypeString());
                                    node.Nodes.Add(detailsType);
                                }
                                detailsNode = new TreeNode(detail.Info + "：" + detail.Remark);
                                detailsNode.Tag = detail;
                                detailsNode.ContextMenuStrip = contextMenuStrip_linkman_details;
                                detailsType.Nodes.Add(detailsNode);
                            }
                        }
                        node.Tag = linkman;
                        node.ContextMenuStrip = contextMenuStrip_linkman_node;
                        groupNode.Nodes.Add(node);
                    }
                }
                groupNode.Tag = group;
                groupNode.ContextMenuStrip = contextMenuStrip_linkman_group;
                treeView_linkman.Nodes.Add(groupNode);
            }
        }

        private void button_new_linkman_group_Click(object sender, EventArgs e)
        {
            editLinkmanGroup(null);
        }

        private void button_new_linkman_Click(object sender, EventArgs e)
        {
            editLinkman(null, null);
        }

        private void editLinkman(Linkman linkman, LinkmanGroup group)
        {
            if (linkman == null)
            {
                linkman = new Linkman();
            }
            if (group != null)
            {
                linkman.GroupId = group.ID;
            }

            LinkmanEditForm dialog = new LinkmanEditForm(linkman);
            DialogResult dr = dialog.ShowDialog(this);
            switch (dr)
            {
                case DialogResult.OK:
                    loadLinkmanData();
                    MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                case DialogResult.Retry:
                    MessageBox.Show("保存失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                default: break;
            }
            dialog.Dispose();
        }

        private void button_refresh_linkman_Click(object sender, EventArgs e)
        {
            loadLinkmanData();
        }

        private void button_delete_all_linkman_Click(object sender, EventArgs e)
        {
            deleteAllChildrenByTableName(DBHelper.TABLE_LINKMAN);
        }

        private void contextMenuStrip_linkman_group_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (e.ClickedItem.Equals(toolStripMenuItem_add_linkman))
            {
                editLinkman(null, (LinkmanGroup)treeView_linkman.SelectedNode.Tag);
            }
            else if (e.ClickedItem.Equals(toolStripMenuItem_edit_linkman_group))
            {
                editLinkmanGroup((LinkmanGroup)treeView_linkman.SelectedNode.Tag);
            }
            else if (e.ClickedItem.Equals(toolStripMenuItem_delete_linkman_group))
            {
                DialogResult dr = MessageBox.Show("确定删除分组【"
                   + ((LinkmanGroup)treeView_linkman.SelectedNode.Tag).Name
                   + "】吗？该操作不可撤销，此分组下的所有联系人也将被一并删除。"
                   , "确认删除", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (dr == DialogResult.OK)
                {
                    if (((LinkmanGroup)treeView_linkman.SelectedNode.Tag).delete(true, true))
                    {
                        loadLinkmanData();
                        MessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("删除失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }

        private void editLinkmanGroup(LinkmanGroup tag)
        {
            LinkmanGroupEditForm dialog = new LinkmanGroupEditForm(tag);
            DialogResult dr = dialog.ShowDialog(this);
            switch (dr)
            {
                case DialogResult.OK:
                    loadLinkmanData();
                    MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                case DialogResult.Retry:
                    MessageBox.Show("保存失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                default: break;
            }
            dialog.Dispose();
        }

        private void treeView_linkman_MouseClick(object sender, MouseEventArgs e)
        {
            treeView_linkman.SelectedNode = treeView_linkman.GetNodeAt(new Point(e.X, e.Y));
        }

        private void contextMenuStrip_linkman_node_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (e.ClickedItem.Equals(toolStripMenuItem_add_linkman_details))
            {
                editLinkmanDetails(null, (Linkman)treeView_linkman.SelectedNode.Tag);
            }
            else if (e.ClickedItem.Equals(toolStripMenuItem_edit_linkman))
            {
                editLinkman((Linkman)treeView_linkman.SelectedNode.Tag, null);
            }
            else if (e.ClickedItem.Equals(toolStripMenuItem_delete_linkman))
            {
                DialogResult dr = MessageBox.Show("确定删除联系人【"
                    + ((Linkman)treeView_linkman.SelectedNode.Tag).Name
                    + "】吗？该操作不可撤销，此联系人的所有联系方式也将被一并删除。"
                    , "确认删除", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (dr == DialogResult.OK)
                {
                    if (((Linkman)treeView_linkman.SelectedNode.Tag).delete(true))
                    {
                        loadLinkmanData();
                        MessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("删除失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }

        private void editLinkmanDetails(LinkmanDetails details, Linkman linkman)
        {
            if (details == null)
            {
                details = new LinkmanDetails();
            }
            if (linkman != null)
            {
                details.LinkmanId = linkman.ID;
            }

            LinkmanDetailsEditForm dialog = new LinkmanDetailsEditForm(details);
            DialogResult dr = dialog.ShowDialog(this);
            switch (dr)
            {
                case DialogResult.OK:
                    loadLinkmanData();
                    MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                case DialogResult.Retry:
                    MessageBox.Show("保存失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                default: break;
            }
            dialog.Dispose();
        }

        private void contextMenuStrip_linkman_details_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (e.ClickedItem.Equals(toolStripMenuItem_edit_linkman_details))
            {
                editLinkmanDetails((LinkmanDetails)treeView_linkman.SelectedNode.Tag, null);
            }
            else if (e.ClickedItem.Equals(toolStripMenuItem_delete_linkman_details))
            {
                DialogResult dr = MessageBox.Show("确定删除联系方式【"
                    + ((LinkmanDetails)treeView_linkman.SelectedNode.Tag).Info
                    + "】吗？该操作不可撤销。", "确认删除",
                    MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (dr == DialogResult.OK)
                {
                    if (((LinkmanDetails)treeView_linkman.SelectedNode.Tag).delete())
                    {
                        loadLinkmanData();
                        MessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("删除失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }

        #endregion

        #region tools

        private void loadToolsData()
        {
            List<Tool> list = null;
            TreeNode groupNode = null;
            TreeNode toolNode = null;

            toolGroupList = DBHelper.listAllToolGroup();
            toolList = DBHelper.listAllTools();

            treeView_tools.Nodes.Clear();
            foreach (ToolGroup group in toolGroupList)
            {
                groupNode = new TreeNode(group.Title + "：" + group.Remark);
                list = (List<Tool>)toolList[group.ID];
                if (null != list)
                {
                    foreach (Tool tool in list)
                    {
                        toolNode = new TreeNode(tool.Name + "：" + tool.Remark);
                        toolNode.Tag = tool;
                        toolNode.ContextMenuStrip = contextMenuStrip_tool_node;
                        groupNode.Nodes.Add(toolNode);
                    }
                }
                groupNode.Tag = group;
                groupNode.ContextMenuStrip = contextMenuStrip_tool_group;
                treeView_tools.Nodes.Add(groupNode);
            }
        }

        private void button_add_tool_group_Click(object sender, EventArgs e)
        {
            editToolGroup(null);
        }

        private void editToolGroup(ToolGroup group)
        {
            ToolGroupEditForm dialog = new ToolGroupEditForm(group);
            DialogResult dr = dialog.ShowDialog(this);
            switch (dr)
            {
                case DialogResult.OK:
                    loadToolsData();
                    MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                case DialogResult.Retry:
                    MessageBox.Show("保存失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                default: break;
            }
            dialog.Dispose();
        }

        private void button_add_tools_Click(object sender, EventArgs e)
        {
            editTool(null, null);
        }

        private void editTool(Tool tool, ToolGroup group)
        {
            if (tool == null)
            {
                tool = new Tool();
            }
            if (group != null)
            {
                tool.GroupId = group.ID;
            }

            ToolEditForm dialog = new ToolEditForm(tool);
            DialogResult dr = dialog.ShowDialog(this);
            switch (dr)
            {
                case DialogResult.OK:
                    loadToolsData();
                    MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                case DialogResult.Retry:
                    MessageBox.Show("保存失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                default: break;
            }
            dialog.Dispose();
        }

        private void button_refresh_tools_Click(object sender, EventArgs e)
        {
            loadToolsData();
        }

        private void button_delete_all_tools_Click(object sender, EventArgs e)
        {
            deleteAllChildrenByTableName(DBHelper.TABLE_TOOL);
        }

        private void contextMenuStrip_tool_group_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (e.ClickedItem.Equals(toolStripMenuItem_add_tool))
            {
                editTool(null, (ToolGroup)treeView_tools.SelectedNode.Tag);
            }
            else if (e.ClickedItem.Equals(toolStripMenuItem_edit_tool_group))
            {
                editToolGroup((ToolGroup)treeView_tools.SelectedNode.Tag);
            }
            else if (e.ClickedItem.Equals(toolStripMenuItem_delete_tool_group))
            {
                DialogResult dr = MessageBox.Show("确定删除分组【"
                    + ((ToolGroup)treeView_tools.SelectedNode.Tag).Title
                    + "】吗？该操作不可撤销，此分组下的所有记录也将被一并删除。"
                    , "确认删除", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (dr == DialogResult.OK)
                {
                    if (((ToolGroup)treeView_tools.SelectedNode.Tag).delete(true))
                    {
                        loadToolsData();
                        MessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("删除失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }

        private void contextMenuStrip_tool_node_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (e.ClickedItem.Equals(toolStripMenuItem_edit_tool))
            {
                editTool((Tool)treeView_tools.SelectedNode.Tag, null);
            }
            else if (e.ClickedItem.Equals(toolStripMenuItem_delete_tool))
            {
                DialogResult dr = MessageBox.Show("确定删除记录【"
                    + ((Tool)treeView_tools.SelectedNode.Tag).Name
                    + "】吗？该操作不可撤销。", "确认删除"
                    , MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (dr == DialogResult.OK)
                {
                    if (((Tool)treeView_tools.SelectedNode.Tag).delete())
                    {
                        loadToolsData();
                        MessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("删除失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }

        private void treeView_tools_MouseClick(object sender, MouseEventArgs e)
        {
            treeView_tools.SelectedNode = treeView_tools.GetNodeAt(new Point(e.X, e.Y));
        }

        #endregion

        #region account

        public void loadAccountData()
        {
            List<Account> list = null;
            TreeNode groupNode = null;
            TreeNode accountNode = null;

            accountGroupList = DBHelper.listAllAccountGroup();
            accountList = DBHelper.listAllAccount();

            treeView_account.Nodes.Clear();
            foreach (AccountGroup group in accountGroupList)
            {
                groupNode = new TreeNode(group.Title + "：" + group.Remark);
                list = (List<Account>)accountList[group.ID];
                if (null != list)
                {
                    foreach (Account account in list)
                    {
                        accountNode = new TreeNode(account.Title);
                        accountNode.Nodes.Add(new TreeNode("名称：" + account.Title));
                        accountNode.Nodes.Add(new TreeNode("登录名：" + account.Name));
                        accountNode.Nodes.Add(new TreeNode("密码：" + account.Password));
                        accountNode.Nodes.Add(new TreeNode("备注：" + account.Remark));
                        accountNode.Tag = account;
                        accountNode.ContextMenuStrip = contextMenuStrip_account_node;
                        groupNode.Nodes.Add(accountNode);
                    }
                }
                groupNode.Tag = group;
                groupNode.ContextMenuStrip = contextMenuStrip_account_group;
                treeView_account.Nodes.Add(groupNode);
            }
        }

        private void button_add_account_group_Click(object sender, EventArgs e)
        {
            editAccountGroup(null);
        }

        private void editAccountGroup(AccountGroup group)
        {
            AccountGroupEditForm dialog = new AccountGroupEditForm(group);
            DialogResult dr = dialog.ShowDialog(this);
            switch (dr)
            {
                case DialogResult.OK:
                    loadAccountData();
                    MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                case DialogResult.Retry:
                    MessageBox.Show("保存失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                default: break;
            }
            dialog.Dispose();
        }

        private void button_add_account_Click(object sender, EventArgs e)
        {
            editAccount(null, null);
        }

        private void editAccount(Account account, AccountGroup group)
        {
            if (account == null)
            {
                account = new Account();
            }
            if (group != null)
            {
                account.GroupId = group.ID;
            }

            AccountEditForm dialog = new AccountEditForm(account);
            DialogResult dr = dialog.ShowDialog(this);
            switch (dr)
            {
                case DialogResult.OK:
                    loadAccountData();
                    MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                case DialogResult.Retry:
                    MessageBox.Show("保存失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                default: break;
            }
            dialog.Dispose();
        }

        private void button_refresh_account_Click(object sender, EventArgs e)
        {
            loadAccountData();
        }

        private void button_delete_all_acount_Click(object sender, EventArgs e)
        {
            deleteAllChildrenByTableName(DBHelper.TABLE_ACCOUNT);
        }

        private void treeView_account_MouseClick(object sender, MouseEventArgs e)
        {
            treeView_account.SelectedNode = treeView_account.GetNodeAt(new Point(e.X, e.Y));
        }

        private void contextMenuStrip_account_group_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (e.ClickedItem.Equals(toolStripMenuItem_add_account))
            {
                editAccount(null, (AccountGroup)treeView_account.SelectedNode.Tag);
            }
            else if (e.ClickedItem.Equals(toolStripMenuItem_edit_account_group))
            {
                editAccountGroup((AccountGroup)treeView_account.SelectedNode.Tag);
            }
            else if (e.ClickedItem.Equals(toolStripMenuItem_delete_account_group))
            {
                DialogResult dr = MessageBox.Show("确定删除分组【"
                    + ((AccountGroup)treeView_account.SelectedNode.Tag).Title
                    + "】吗？该操作不可撤销，此分组下的所有记录也将被一并删除。"
                    , "确认删除", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (dr == DialogResult.OK)
                {
                    if (((AccountGroup)treeView_account.SelectedNode.Tag).delete(true))
                    {
                        loadAccountData();
                        MessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("删除失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }

        private void contextMenuStrip_account_node_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (e.ClickedItem.Equals(toolStripMenuItem_edit_account))
            {
                editAccount((Account)treeView_account.SelectedNode.Tag, null);
            }
            else if (e.ClickedItem.Equals(toolStripMenuItem_delete_account))
            {
                DialogResult dr = MessageBox.Show("确定删除记录【"
                    + ((Account)treeView_account.SelectedNode.Tag).Title
                    + "】吗？该操作不可撤销。", "确认删除"
                    , MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (dr == DialogResult.OK)
                {
                    if (((Account)treeView_account.SelectedNode.Tag).delete())
                    {
                        loadAccountData();
                        MessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("删除失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }

        #endregion
    }
}